package condominio.servlet.cuota;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominio.excepcion.DAOExcepcion;
import condominio.modelo.Persona;
import condominio.negocio.CuotaNegocio;

/**
 * Servlet implementation class CuotaInsertarServlet
 */
@WebServlet("/CuotaInsertarServlet")
public class CuotaInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CuotaInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Persona oUsuario = (Persona)session.getAttribute("USUARIO_ACTUAL");
		
		if(oUsuario == null){
			request.getRequestDispatcher("error.jsp").forward(request, response);
			return;
		}
		
		String x = request.getParameter("anio");
		String y = request.getParameter("mes");
		double z = Double.parseDouble(request.getParameter("importe"));
		
		/*DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String s = df.format(Fecha);
		*/
		
		SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd");
		String strFecha = request.getParameter("aniov") + "-" + request.getParameter("mesv") + "-" + request.getParameter("diav");
		java.sql.Date fecha = null;

		Date fechaSQL = null;
		
		fecha = new java.sql.Date(Integer.parseInt(request.getParameter("aniov"))-1900
				,Integer.parseInt(request.getParameter("mesv")),Integer.parseInt(request.getParameter("diav")));

//		java.sql.Date theDate = new java.sql.Date(Integer.parseInt(startDate.substring(6, 10))- 1900, Integer.parseInt(startDate.substring(3, 5))-1,Integer.parseInt(startDate.substring(0, 2))); 

		
		try {
			fechaSQL = new Date(formatoDelTexto.parse(strFecha).getTime());
		    System.out.println(fechaSQL.toString());
		    } catch (ParseException ex) {
		                       ex.printStackTrace();
		    }
		
		CuotaNegocio negocio = new CuotaNegocio();
		try {
			negocio.InsertaCuota(x, y, z, fechaSQL);
			response.sendRedirect(request.getContextPath() + "/menu.jsp");
		} catch (DAOExcepcion e) {
			session.setAttribute("ERROR_MSG", e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("/inscripcion_cuota.jsp");
			rd.forward(request, response);
		}
	}
}
